package com.study.java2.oauth.service;

//import com.study.java2.oauth.dao.UsersRepository;

import com.study.java2.oauth.entity.Users;
import com.study.java2.oauth.mapper.UsersMapper;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;

@Service
public class JdbcUserDetailsServiceImpl implements UserDetailsService {


    @Resource
    private UsersMapper usersMapper;



    /**
     *根据username查询出该用户的所有信息，封装成USerDetails类型的对象返回，至于密码，框架会自动匹配
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Users users = usersMapper.findByUsername(username);
        return new User(users.getUsername(),users.getPassword(),new ArrayList<>());
    }

    public UserDetails loadUserByMobile(String mobile) {
        Users users = usersMapper.loadUserByMobile(mobile);
        if (users == null) throw new UsernameNotFoundException("用户不存在");
        return new User(users.getUsername(),users.getPassword(),new ArrayList<>());
    }
}
